/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.niclun.projectwebshop.nbp;

import edu.chl.niclun.projectwebshop.nbp.Entities.Movie;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *Used to save all the movies that the user put in the cart
 * @author niclas
 */
@ManagedBean(name="CartBean")
@SessionScoped
public class CartModelBean {
    private static final int SHIPPING_COST = 80;
    
    private Map<Movie, Integer> cart;
    
    /** Creates a new instance of CartModelBean */
    public CartModelBean() {
        cart = new TreeMap<Movie, Integer>();
    }
    
    //Add a movie to the cart
    public void addToCart(Movie m) {
        if(cart.containsKey(m)) {
            int count = cart.get(m) + 1;
            cart.put(m, count);
        } else {
            cart.put(m, 1);
        }
    }
   
    //Remove a movie from the cart
    public void removeFromCart(Movie m) {
        if(cart.containsKey(m)) {
            int count = cart.get(m);
            if(count > 1) {
                cart.put(m, count-1);
            }else {
                cart.remove(m);
            }
        }
    }
    
    public void setCart(Map<Movie, Integer> cart) {
        this.cart = cart;
    }
    
    public Map<Movie, Integer> getCart() {
        return cart;
    }
    
    //get number of items in the cart
    public int getNrOfProducts() {
        return cart.size();
    }
    
    
    public int getCount() {
        int count = 0;
        for(Integer i : cart.values())
            count += i;
        
        return count;
    }
    
    public int getCount(Movie m) {
        return cart.get(m);
    }
    
    //gets the list of movies in the cart
    public ArrayList<Movie> getMovieList() {
        Movie[] movieArr = new Movie[cart.keySet().size()];
        cart.keySet().toArray(movieArr);
        return new ArrayList<Movie>(Arrays.asList(movieArr));
    }
    
    //gets the total cost for this movie including the qty
    public int getTotalCost(Movie movie) {
        return movie.getPrice() * getCount(movie);   
    }
    
    //get the total cost for the cart
    public int getTotalCost() {
        int total = 0;
        Iterator<Entry<Movie, Integer>> it = cart.entrySet().iterator();
        
        Entry<Movie, Integer> entry;
        while(it.hasNext()) {
            entry = it.next();
            total += entry.getKey().getPrice() * entry.getValue();
        }
        
        return total;
    }
    
    // mepty the cart
    public void emptyCart() {
        cart.clear();
    }
    
    //get the shipping cost
    public int getShippingCost() {
        if(getCount() > 0)
            return SHIPPING_COST;
        else
            return 0;
    }
    
    // gets the total cost for the cart including shipping cost
    public int getTotCostIncShipping() {
        return getTotalCost() + getShippingCost();
    }
    
    // String representation of the cart
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        Iterator< Entry<Movie, Integer> > it = cart.entrySet().iterator();
        Entry<Movie, Integer> entry;
        while(it.hasNext()) {
            entry = it.next();
            builder.append(entry.getKey().getTitle()).append("  ").append(entry.getKey().getPrice()).append(" kr\n");
            builder.append("Qty:  ").append(entry.getValue()).append("\nTot:  ").append(getTotalCost(entry.getKey())).append(" kr");
            builder.append("\n\n");
        }
        
        builder.append("Shipping cost:  ").append(getShippingCost()).append(" kr").append("\n\n");
        builder.append("Total cost:  ").append(getTotCostIncShipping()).append(" kr");
        
        return builder.toString();
    }
}